<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Monoslideshow Controller
 *
 * @package    Monoslideshow
 * @subpackage Controllers
 */
class JmonoslideshowControllerImages extends JmonoslideshowController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'apply', 'save' );
		$this->registerTask( 'add' , 'edit' );
	}// function

	/**
	 * display the edit form
	 * @return void
	 */
	function edit()
	{
		JRequest::setVar( 'view', 'Image' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);
		$model = $this->getModel('Image');
		$model->checkout();
		parent::display();
	}// function

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$mainframe =& JFactory::getApplication();
		$model = $this->getModel('Image');
		$post = JRequest::get('post');
		if ($model->store($post)) {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_MEDIA_SAVED' );
		} else {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_ERROR_SAVING_MEDIA' );
			$mainframe->enqueueMessage($model->getError(), 'error');
		}
		$row = & $model->getRow();
		if (  $this->getTask() == 'apply' ) {
			$link = 'index.php?option=com_jmonoslideshow&controller=images&task=edit&cid[]=' . $row->id ;
		}
		else
		{
			// Check the table in so it can be edited.... we are done with it anyway
			$link = 'index.php?option=com_jmonoslideshow&view=images';
		}
		$model->checkin();
		$this->setRedirect($link, $msg);
	}// function

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel('Image');
		if(!$model->delete()) {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_ERROR_ONE_OR_MORE_IMAGE_COULD_NOT_BE_DELETED' );
		} else {
			$msg = JText::_( 'COM_JMONOSLIDESHOW_MEDIA_DELETED' );
		}

		$this->setRedirect( 'index.php?option=com_jmonoslideshow&view=images', $msg );
	}// function

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Checkin the weblink
		$model = $this->getModel('Image');
		$model->checkin();
		
		$msg = JText::_( 'COM_JMONOSLIDESHOW_OPERATION_CANCELLED' );
		$this->setRedirect( 'index.php?option=com_jmonoslideshow&view=images', $msg );
	}// function
	
	function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'COM_JMONOSLIDESHOW_SELECT_AN_ITEM_TO_PUBLISH' ) );
		}

		$model = $this->getModel('Image');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_jmonoslideshow&view=images' );
	}
	
	function unpublish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'COM_JMONOSLIDESHOW_SELECT_AN_ITEM_TO_UNPUBLISH' ) );
		}

		$model = $this->getModel('Image');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_jmonoslideshow&view=images' );
	}
	
	function orderup()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$model = $this->getModel('Image');
		$model->move(-1);

		$this->setRedirect( 'index.php?option=com_jmonoslideshow&view=images');
	}

	function orderdown()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$model = $this->getModel('Image');
		$model->move(1);

		$this->setRedirect( 'index.php?option=com_jmonoslideshow&view=images');
	}

	function saveorder()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'order', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);

		$model = $this->getModel('Image');
		$model->saveorder($cid, $order);

		$msg = JText::_( 'COM_JMONOSLIDESHOW_NEW_ORDERING_SAVED' );
		$this->setRedirect( 'index.php?option=com_jmonoslideshow&view=images', $msg );
	}


	
}// class